package com.winz.cultureapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.CommunityDesBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.ApplyCommunityReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;
import com.winz.cultureapp.widge.SelSexDialog;
import com.winz.cultureapp.widge.TimerSelector;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 加入社团
 */
public class JoinCommunityActivity extends BaseAppCompatActivity implements TimerSelector.OkClickListener {
    private static final int BIRTH_TIME = 0x000001;

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.et_name)
    EditText mEt_name;
    @BindView(R.id.et_tell)
    EditText mEt_tell;
    @BindView(R.id.tv_sex)
    TextView mTv_sex;
    @BindView(R.id.tv_birth)
    TextView mTv_birthday;
    @BindView(R.id.et_school)
    EditText mEt_school;
    @BindView(R.id.et_degree)
    EditText mEt_degree;
    @BindView(R.id.et_work)
    EditText mEt_work;
    @BindView(R.id.et_leisure)
    EditText mEt_leisure;
    @BindView(R.id.et_hobby)
    EditText mEt_hobby;
    @BindView(R.id.et_introduce)
    EditText mEt_Introduce;

    private SelSexDialog mSel_sex;
    private String sex; // 当前选择性别记录

    private TimerSelector ts_birht;
    private String birthDate; // 当前选择的日期
    private long birth;
    private String leagueId;

    @Override
    protected int contentView() {
        return R.layout.activity_join_community;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText("申请资料");

        mSel_sex = new SelSexDialog(this);


        ts_birht = new TimerSelector(this, "yyyy年MM月dd日");
        ts_birht.setOkClickListener(this);
    }

    @Override
    protected void initEvents() {
        mSel_sex.setOkButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSel_sex.dismiss();
                sex = mSel_sex.getSelSex();
                mTv_sex.setText(sex);
            }
        });
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                leagueId = intent.getExtras().getString(Constants.KEY.COMMUNITY.ITEM_ID,"");
            }
        }
    }

    /**
     * 判断是否全部填写
     *
     * @return
     */
    private boolean isAllRight() {
        if (TextUtils.isEmpty(leagueId)){
            showMsg("社团ID不存在");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_name))) {
            showMsg("请输入姓名");
            return false;
        }
        if (TextUtils.isEmpty(getEditTextStr(mEt_tell))) {
            showMsg("请输入手机号");
            return false;
        }
        if (TextUtils.isEmpty(sex)) {
            showMsg("请选择性别");
            return false;
        }
        if (TextUtils.isEmpty(birthDate)){
            showMsg("请选择出生年月");
            return false;
        }
        return true;
    }

    @OnClick({R.id.fl_back, R.id.ll_sex, R.id.btn_submit, R.id.ll_birth})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.ll_sex:
                mSel_sex.show();
                break;
            case R.id.ll_birth:
                ts_birht.show(mTv_birthday, BIRTH_TIME);
                break;
            case R.id.btn_submit:
                if (isAllRight()) {
                    commit();
                }
                break;
        }
    }

    /**
     * 提交
     */
    private void commit() {
        showProgressDialog();
        HttpUitls.onPostByToken("league_apply", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                new ApplyCommunityReq(birthDate,
                        getEditTextStr(mEt_work),
                        getEditTextStr(mEt_Introduce),
                        getEditTextStr(mEt_degree),
                        getEditTextStr(mEt_leisure),
                        SharePrefsUtil.getInstance().getString(Constants.USER_INFO.HEAD_IMAGE),
                        leagueId,
                        getEditTextStr(mEt_tell),
                        getEditTextStr(mEt_name),
                        getEditTextStr(mEt_school),
                        "男".equals(sex)?"1":"2",
                        getEditTextStr(mEt_hobby)),
                new OnResponeListener<CommunityDesBean>() {
                    @Override
                    public void onSuccess(CommunityDesBean bean) {
                        Logger.d("league_apply==>>", JsonUtil.getJsonFromObj(bean));
                        HttpUitls.isExpire(bean.code);
                        if (bean.code == 0) {
                            showMsg("申请成功");
                            startActivity(MyCommunityActivity.class,true);
                        } else {
                            showMsg(bean.msg);
                        }
                        dismissProgressDialog();
                    }

                    @Override
                    public void onFailed(String error) {
                        dismissProgressDialog();
                        Logger.d("league_apply==>>", error);
                    }
                });
    }

    @Override
    public void okClick(View view, String date, long time, int flag) {
        if (flag == BIRTH_TIME) {
            mTv_birthday.setText(date);
            birthDate = date;
            birth = time;
        }
    }
}
